/** @odoo-module **/
import {Toast} from './components/Toast.js';
import {VideoModal} from './components/VideoModal.js';



const {
    markup,
    Component,
    onWillStart,
    useEnv,
    useSubEnv,
    useChildSubEnv,
    useRef,
    useState,
    mount,
    xml,
    whenReady,
    onMounted,
    onError,
    loadFile,
    onWillUpdateProps,
    useEffect,
    useComponent,
    EventBus
} = owl;

const weekStr = '日一二三四五六';


class Root extends Component {

    setup() {

        console.log(this.props);

        this.bus = new EventBus();

        useSubEnv({
            bus: this.bus
        });

        useChildSubEnv({// 只会在组件的子组件中挂载公共数据及方法
            closeToast: () => {
                this.state.toastOpen = false;
                this.state.toastData = {};
            },
            closeVideoModal:()=>{
                this.state.VideoModalOpen = false;
                this.state.VideoModalData = {};
            },
            openToast:(data)=>{
                this.state.toastOpen = true;
                this.state.toastData = {...data};
            },
        });

        let date = new Date();

        let today_str = `${date.getFullYear()}年${date.getMonth()+1}月${date.getDate()} 星期${weekStr[date.getDay()]}`;

        this.index = {};
        this.state = useState({
            loading:false,
            isMenuMode: true,
            toastOpen: false,
            toastData: {},//提示消息
            menuList:this.props.menu_obj_list,
            log_out_menu:{},
            today_str:today_str,
            VideoModalOpen:false,
            VideoModalData:{}
        });

        onWillStart(async () => await this.LoadData());
        onWillUpdateProps(async next_props => await this.LoadData());
        // onMounted(()=>{});

        this.handleMenuClick = _.debounce(this.menuClick,300);
        this.handleOpenVideoModal = _.debounce(this.openVideoModal,300);


    }
    async LoadData() {

    }

    async openVideoModal(e){
        let url = e.target.dataset.url;
        this.state.VideoModalOpen = true;
        this.state.VideoModalData = {
            src:url
        };
    }

    async menuClick(e){
        // console.log('menuClick');
        let title = e.target.dataset.title;
        let parenttitle = e.target.dataset.parenttitle;
        let url = e.target.dataset.url;

        if(!title){
           return;
        }

        console.log(title);
        console.log(parenttitle);
        console.log(url);


        let key = `${parenttitle}_${title}`;
        if (this.props.GotoUrl && url) {
            await this.props.GotoUrl(url);
        }
    }
}


Root.template = 'Gmm_menu_help_Root';


Root.props = {
    menu_obj_list: {type: Array},
    menu_url_dict: {type: Object},
    LoadData: {type: Function, optional: true},
    GotoUrl: {type: Function, optional: true},
};


Root.components = {
    Toast,
    VideoModal
};


export {
    Root
}
